﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microhard.ApproveFlow.Buz.Utility;
using Microhard.Core;

namespace Microhard.Permission.MVCFilter.MVCFilter
{
    public class MVCFilterAtrForWorkflow : ActionFilterAttribute
    {
        private string _workflowCode;

        public MVCFilterAtrForWorkflow(string workFlowCode)
        {
            _workflowCode = workFlowCode;
        }

        public void RedirectToPage(ActionExecutingContext filterContext, string code)
        {
            var ad = new ViewResult();
            ad.ViewName = "WorkflowStartError";
            ad.ViewData["errorCode"] = code;
            filterContext.Result = ad;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var businessPKID = Guid.Parse(filterContext.ActionParameters["identity"].ToString());
            //发起流程
            var rtn = FlowHelper.StartWorkflow(_workflowCode, businessPKID);
            if (!rtn.IsSucceed)
            {
                Log.Error("StartJHWorkflowTransFilter", "发起流程失败," + _workflowCode + "," + businessPKID);
                this.RedirectToPage(filterContext, rtn.ErrorMsgTxt);
            }
        }
    }
}
